package com.ruoyi.travel.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.travel.domain.TravelProduct;
import com.ruoyi.travel.service.ITravelProductService;

/**
 * 旅游产品Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/travel/product")
public class TravelProductController extends BaseController
{
    @Autowired
    private ITravelProductService travelProductService;

    /**
     * 查询旅游产品列表
     */
    @RequiresPermissions("travel:product:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelProduct travelProduct)
    {
        startPage();
        List<TravelProduct> list = travelProductService.selectTravelProductList(travelProduct);
        return getDataTable(list);
    }

    /**
     * 小程序查询旅游产品列表（无需权限）
     */
    @GetMapping("/app/list")
    public AjaxResult appList(TravelProduct travelProduct)
    {
        travelProduct.setStatus("3"); // 默认只查询已上线（status=3表示已上线）
        List<TravelProduct> list = travelProductService.selectTravelProductList(travelProduct);
        return success(list);
    }

    /**
     * 小程序查询热门产品（前5条，按id排序，无需权限）
     * 使用 /app/hot/list 避免与 /app/{productId} 路由冲突
     */
    @GetMapping("/app/hot/list")
    public AjaxResult appHotProducts()
    {
        List<TravelProduct> list = travelProductService.selectHotProducts();
        return success(list);
    }

    /**
     * 小程序搜索旅游产品（支持按目的地、品类、产品名称搜索，无需权限）
     */
    @GetMapping("/app/search")
    public AjaxResult appSearch(String keyword)
    {
        TravelProduct travelProduct = new TravelProduct();
        travelProduct.setStatus("3"); // 默认只查询已上线（status=3表示已上线）
        travelProduct.setKeyword(keyword); // 设置搜索关键词
        List<TravelProduct> list = travelProductService.searchTravelProducts(travelProduct);
        return success(list);
    }

    /**
     * 获取旅游产品详细信息
     */
    @RequiresPermissions("travel:product:query")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(travelProductService.selectTravelProductById(productId));
    }

    /**
     * 小程序获取旅游产品详细信息（无需权限）
     * 使用正则表达式限制productId必须是数字，避免匹配到 "hot" 等字符串
     */
    @GetMapping(value = "/app/{productId:[0-9]+}")
    public AjaxResult appGetInfo(@PathVariable("productId") Long productId)
    {
        return success(travelProductService.selectTravelProductById(productId));
    }

    /**
     * 新增旅游产品
     */
    @RequiresPermissions("travel:product:add")
    @Log(title = "旅游产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelProduct travelProduct)
    {
        travelProduct.setCreateBy(SecurityUtils.getUsername());
        return toAjax(travelProductService.insertTravelProduct(travelProduct));
    }

    /**
     * 修改旅游产品
     */
    @RequiresPermissions("travel:product:edit")
    @Log(title = "旅游产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelProduct travelProduct)
    {
        travelProduct.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(travelProductService.updateTravelProduct(travelProduct));
    }

    /**
     * 删除旅游产品
     */
    @RequiresPermissions("travel:product:remove")
    @Log(title = "旅游产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(travelProductService.deleteTravelProductByIds(productIds));
    }
}

